package util

import (
	"github.com/gin-gonic/gin"
	"io/fs"
	"net/http"
	"strings"
)

func SetFsView(engine *gin.RouterGroup, indexHtml string, parent string, sourceFs fs.FS) error {
	httpFs := http.FS(sourceFs)
	err := fs.WalkDir(sourceFs, parent, func(path string, d fs.DirEntry, err error) error {
		if err == nil && !d.IsDir() {
			engine.StaticFileFS(strings.TrimPrefix(path, parent), path, httpFs)
		}
		return err
	})
	if err != nil {
		return err
	}
	indexHtmlFunc := func(ctx *gin.Context) {
		ctx.Header("content-type", "text/html;charset=utf-8")
		ctx.String(http.StatusOK, indexHtml)
	}
	engine.GET("/", indexHtmlFunc)
	engine.GET("/setting", indexHtmlFunc)
	engine.GET("/setting/local_setting", indexHtmlFunc)
	engine.GET("/setting/net_disk_setting", indexHtmlFunc)
	engine.GET("/setting/http_setting", indexHtmlFunc)
	engine.GET("/setting/log_setting", indexHtmlFunc)
	return nil
}
